﻿using CGT.API.Common;
using CGT.API.Models;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CGT.API.Controllers
{
    /// <summary>
    /// 公共的接口
    /// </summary>
    public class CommonController : ApiController
    {

        /// <summary>
        /// 根据城市获取首页显示的业务
        /// </summary>
        /// <param name="cityId">城市</param>
        /// <returns></returns>
        public ResultData<List<BizTypeGroupModel>> GetIndexBizType(int cityId)
        {
            List<BizTypeGroupModel> lst = new List<BizTypeGroupModel>();
            
            DataTable dt = BizTypeOperate.GetIndexBizType(cityId);
            //对查询的业务进行分组
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                BizTypeGroupModel model = new BizTypeGroupModel();
                model.title = EConvert.ToString(dt.Rows[i]["MasterBizTypeName"]);
                for (int j = i; j < dt.Rows.Count; j++)
                {
                    i = j;
                    if (model.title.Equals(EConvert.ToString(dt.Rows[j]["MasterBizTypeName"])))
                    {
                        model.child.Add(new BizTypeMode()
                        {
                            BizTypeID = EConvert.ToInt(dt.Rows[j]["BizTypeID"]),
                            MasterBizTypeID = EConvert.ToInt(dt.Rows[j]["MasterBizTypeID"]),
                            MasterBizTypeName = EConvert.ToString(dt.Rows[j]["MasterBizTypeName"]),
                            BizTypeName = EConvert.ToString(dt.Rows[j]["BizTypeName"]),
                            BizTypeSort = EConvert.ToInt(dt.Rows[j]["BizTypeSort"]),
                            BizTypeIcon = EConvert.ToString(dt.Rows[j]["BizTypeIcon"]),
                            BizTypePinYin = EConvert.ToString(dt.Rows[j]["BizTypePinYin"]),
                            CallBack = "addRegister"
                        });
                    }
                    else
                    {
                        i--;
                        break;
                    }
                }
                if (model.child.Count > 0)
                {
                    model.child.Add(new BizTypeMode()
                    {
                        BizTypeID = -1,
                        MasterBizTypeID = model.child[0].MasterBizTypeID,
                        MasterBizTypeName = "更多",
                        BizTypeName = "更多",
                        BizTypeSort = 1000,
                        BizTypeIcon = "icon-gengduo",
                        BizTypePinYin = model.child[0].BizTypePinYin,
                        CallBack = "addRegister"
                    });
                }
                lst.Add(model);
            }
            return ResultData<List<BizTypeGroupModel>>.Success(lst);
        }

        /// <summary>
        /// 根据城市和业务获取价格
        /// </summary>
        /// <param name="cityId">城市</param>
        /// <param name="bizTypeId">业务</param>
        /// <returns></returns>
        public ResultData<List<NetWorkFeeConfigInfo>> GetOrderPrice(int cityId, int bizTypeId)
        {
            List<NetWorkFeeConfigInfo> infos = NetWorkFeeConfigOperate.GetFeeConfigs(cityId, bizTypeId);
            return ResultData<List<NetWorkFeeConfigInfo>>.Success(infos);
        }

        /// <summary>
        /// 根据城市和业务获取指南
        /// </summary>
        /// <param name="cityId">城市</param>
        /// <param name="bizTypeId">业务</param>
        /// <returns></returns>
        public ResultData<List<string>> GetGuidance(int cityId, int bizTypeId)
        {
            //查询办事指南
            GuidanceInfo info = GuidanceOperate.GetGuidances(cityId, bizTypeId);
            if (info == null || string.IsNullOrEmpty(info.GuidanceWxMini))
            {
                return ResultData<List<string>>.Error("暂未配置办事指南");
            }

            //按回车进行分组
            List<string> lst = new List<string>();
            string[] strArr = info.GuidanceWxMini.Split(new string[] { "\n" }, StringSplitOptions.None);
            for (int i = 0; i < strArr.Length; i++)
            {
                if (!string.IsNullOrEmpty(strArr[i]))
                {
                    lst.Add(strArr[i]);
                }
            }
            return ResultData<List<string>>.Success(lst);
        }

        /// <summary>
        /// 获取快递费用
        /// </summary>
        /// <param name="cityId">城市</param>
        /// <param name="bizTypeId">业务</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<ExpressFeeModel>> GetExpressFee(int cityId, int bizTypeId)
        {
            List<ExpressFeeModel> lst = new List<ExpressFeeModel>();
            DataTable dt = ExpressFeeTplOperate.GetExpressFee(cityId, bizTypeId);
            foreach (DataRow row in dt.Rows)
            {
                lst.Add(new ExpressFeeModel()
                {
                    ExpressFeeTplID = EConvert.ToInt(row["ExpressFeeTplID"]),
                    ExpressCompanyID = EConvert.ToInt(row["ExpressCompanyID"]),
                    ExpressFee = EConvert.ToInt(row["ExpressFee"]),
                    CompanyName = EConvert.ToString(row["CompanyName"]),
                    Logo = EConvert.ToString(row["Logo"])
                });
            }
            return ResultData<List<ExpressFeeModel>>.Success(lst);
        }

        /// <summary>
        /// 根据业务类型和排序获取更多业务类型
        /// </summary>
        /// <param name="masterBizTypeId">主业务类型</param>
        /// <param name="sort">排序</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<BizTypeInfo>> GetMoreBizType(int masterBizTypeId, int sort)
        {
            var infos = BizTypeOperate.GetBizTypes(masterBizTypeId, sort);
            return ResultData<List<BizTypeInfo>>.Success(infos);
        }

        /// <summary>
        /// 获取体检医院
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<HospitalInfo>> GetHospitals(int cityId)
        {
            List<HospitalInfo> list = HospitalOperate.GetByCity(cityId);
            return ResultData<List<HospitalInfo>>.Success(list);
        }

        /// <summary>
        /// 获取开放城市
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<OpenCityModel>> GetAllOpenCity()
        {
            List<OpenCityModel> list = new List<OpenCityModel>();
            DataTable dt = CityOpenOperate.GetAllOpenCity();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(new OpenCityModel()
                {
                    CityId = EConvert.ToInt(row["CityID"]),
                    CityName = EConvert.ToString(row["CityName"]),
                    Initial = EConvert.ToString(row["locationSpellName"]).Substring(0,1).ToUpper(),
                    CitySpellName = EConvert.ToString(row["locationSpellName"])
                });
            }
            return ResultData<List<OpenCityModel>>.Success(list);
        }
    }
}
